<?php
// +----------------------------------------------------------------------
// | Description: 系统员工
// +----------------------------------------------------------------------
// +----------------------------------------------------------------------

namespace app\admin\controller;

use app\admin\model\Rule;
use think\facade\Request;


class Rules extends ApiCommon
{

    /**
     * 用于判断权限
     * @permission 无限制
     * @allow 登录用户可访问
     * @other 其他根据系统设置
    **/
    public function initialize()
    {
        $action = [
            'permission'=>[''],
            'allow'=>['index']
        ];

        event('CheckAuth',$action);
        $request = Request::instance();
        $a       = strtolower($request->action());
        if (!in_array($a, $action['permission'])) {
            parent::initialize();
        }
    }

    /**
     * 权限菜单列表
     * @return \think\response\Json
     * @author Mr.Liu Wuchen
     * @create 2021-05-22 9:21
     */
    public function index()
    {
        $ruleModel = new Rule();
        $data      = $ruleModel->getDataList($this->param);
        return resultArray(['data' => $data]);
    }

    /**
     * 新建规则
     * @return \think\response\Json
     * @author Mr.Liu Wuchen
     * @create 2021-05-22 9:20
     */
    public function save()
    {
        $ruleModel = new Rule();
        $data      = $ruleModel->createData($this->param);
        if (!$data) {
            return resultArray(['error' => $ruleModel->getError()]);
        }
        return resultArray(['data' => '添加成功']);
    }

    /**
     * 编辑规则
     * @return \think\response\Json
     * @author Mr.Liu Wuchen
     * @create 2021-05-22 9:20
     */
    public function update()
    {
        $ruleModel = new Rule();
        $data      = $ruleModel->updateDataById($this->param, $this->param['id'] ?? 0);
        if (!$data) {
            return resultArray(['error' => $ruleModel->getError()]);
        }
        return resultArray(['data' => '编辑成功']);
    }

}
